﻿using System;
using System.Web;
using ASPNETVN.PORTAL.Components.UI;
using WEATHER.Components;
using ASPNETVN.PORTAL.Components.Security;

namespace WEATHER.Modules.MANAGEMENT.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.Update += AddEditItem_Update;
            ButtonCancel = this.btnCancel;
            ButtonUpdate = this.btnUpdate;
            base.OnInit(e);
        }

        private Guid GetModuleID(string weatherType)
        {
            if (weatherType.Substring(0, 4) == "TTNH" || weatherType.Substring(0, 4) == "TVDB")
            {
                return new Guid(this.Settings["WEATHER.DangerousWeather.ModuleID"].ToString());
            }
            else if (weatherType == "ProvinceWeather")
            {
                return new Guid(this.Settings["WEATHER.Province.ModuleID"].ToString());
            }
            else if (weatherType == "AreaWeather")
            {
                return new Guid(this.Settings["WEATHER.Area.ModuleID"].ToString());
            }
            else
            {
                return new Guid(this.Settings["WEATHER.Meteor.ModuleID"].ToString()); 
            }
        }


        void AddEditItem_Update(object sender, EventArgs e)
        {
            Item item = new Item();
            item.ModuleID = GetModuleID(ddlWeatherType.SelectedValue);
            item.WeatherType = ddlWeatherType.SelectedValue == "ProvinceWeather" ? ddlWeatherType.SelectedValue + ddlProvinces.SelectedValue : ddlWeatherType.SelectedValue;
            item.HtmlContent = HttpUtility.HtmlEncode(txtHtmlContent.Value);
            item.Title = txtTitle.Text;
            item.CreatedDate = DateTime.Now;
            item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;            

            if (this.ItemID == 0)
            {
                item.Insert();
            }
            else
            {
                item.ID = this.ItemID;
                item.Update();
            }

            this.ReturnToReferrerPage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Item item = Item.Load(this.ItemID);
                    if (item != null)
                    {
                        txtTitle.Text = item.Title;
                        if (item.WeatherType.Contains("ProvinceWeather"))
                        {
                            ddlWeatherType.SelectedValue = "ProvinceWeather";
                            ddlProvinces.SelectedValue = item.WeatherType.Substring("ProvinceWeather".Length - 1, item.WeatherType.Length - "ProvinceWeather".Length);
                            ddlProvinces.Visible = true;
                        }
                        else
                        {
                            ddlWeatherType.SelectedValue = item.WeatherType;
                        }
                        txtHtmlContent.Value = HttpUtility.HtmlDecode(item.HtmlContent);
                    }
                }
            }
        }

        protected void ddlWeatherType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlProvinces.Visible = ddlWeatherType.SelectedValue == "ProvinceWeather";
        }
    }
}